﻿// <copyright file="ArrayExtensionsTest.cs" company="DotNetLib">
//
// .netlib: http://www.codeplex.com/DotNetLib
//
// This code is released under the New BSD License.
// A copy of this license is available at:
// http://www.codeplex.com/DotNetLib/license
//
// </copyright>

namespace DotNetLib.Tests
{
  using System;
  using global::Microsoft.VisualStudio.TestTools.UnitTesting;

  /// <summary>
  /// This is a test class for DotNetLib.ArrayExtensions.
  /// </summary>
  [TestClass()]
  public class ArrayExtensionsTest : TestBase
  {
    [ClassInitialize()]
    public static void Initialize(TestContext testContext)
    {
    }

    [ClassCleanup()]
    public static void Cleanup()
    {
    }

    /// <summary>
    /// A test for IsNullOrEmpty.
    /// </summary>
    [TestMethod()]
    public void IsNullOrEmptyTest()
    {
      object[] array = null;
      Assert.IsTrue(ArrayExtensions.IsNullOrEmpty(array));

      array = new object[0];
      Assert.IsTrue(ArrayExtensions.IsNullOrEmpty(array));

      array = new object[1] { null };
      Assert.IsFalse(ArrayExtensions.IsNullOrEmpty(array));
    }

    /// <summary>
    /// A test for ToHexString
    /// </summary>
    [TestMethod()]
    public void ToHexStringTest()
    {
      byte[] buf = new byte[] { };
      Assert.AreEqual(string.Empty, buf.ToHexString());

      buf = new byte[] { 0, 255 };
      Assert.AreEqual("00ff", buf.ToHexString());
    }

    [TestMethod()]
    public void TestCombine()
    {
      int[] expected = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

      int[] array1 = new int[] { 1, 2, 3 };
      int[] array2 = new int[] { 4, 5, 6 };
      int[] array3 = new int[] { 7, 8, 9 };
      int[] result = ArrayExtensions.Combine(array1, array2, array3);

      CollectionAssert.AreEqual(expected, result);
    }

    [TestMethod()]
    public void TestToDelimitedString()
    {
      string expected = "1,2,3,4,5,6,7,8,9";
      int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      Assert.AreEqual(expected, array.ToDelimitedString("{0}", ","));

      expected = string.Empty;
      array = null;
      Assert.AreEqual(expected, array.ToDelimitedString("{0}", ", "));

      array = new int[] { };
      Assert.AreEqual(expected, array.ToDelimitedString("{0}", ", "));
    }

    public void AreEqual<T>(T[] expected, T[] actual) where T : IEquatable<T>
    {
      if (expected == null && actual == null)
      {
        return;
      }

      if (expected == null || actual == null)
      {
        throw new AssertFailedException();
      }

      if (expected.Length != actual.Length)
      {
        throw new AssertFailedException();
      }

      for (int index = 0; index < expected.Length; ++index)
      {
        if (!expected[index].Equals(actual[index]))
        {
          throw new AssertFailedException();
        }
      }
    }
  }
}
